// 命名工具类

package common

import (
	"bytes"
	"math/rand"
	"strings"
	"time"
	"unicode"
)

// 字符串首字母转化为大写 AbccDeee -> abccDeee
func StrFirstToLower(str string) string {
	if str == "" {
		return ""
	}
	temp := []byte(str)
	lower := strings.ToLower(string(temp[0]))
	temp[0] = []byte(lower)[0]
	return string(temp)
}

// 字符串首字母转化为大写 AbccDeee -> abccDeee
func StrFirstToUpper(str string) string {
	if str == "" {
		return ""
	}
	temp := []byte(str)
	lower := strings.ToUpper(string(temp[0]))
	temp[0] = []byte(lower)[0]
	return string(temp)
}

// 字符串首字母转化为大写 AbccDeee -> abccDeee
func DbNameToGoName(str string) string {
	if str == "" {
		return ""
	}
	temp := strings.Split(str, "_")
	buffer := bytes.Buffer{}
	for i, v := range temp {
		if i == 0 {
			buffer.WriteString(StrFirstToLower(v))
		} else {
			buffer.WriteString(StrFirstToUpper(v))
		}
	}
	return string(buffer.Bytes())
}

// Go语言命名转换成数据库命名
func GoNameToDbName(str string) string {
	buffer := bytes.Buffer{}
	for i, r := range str {
		if unicode.IsUpper(r) {
			if i > 0 {
				buffer.WriteString("_")
			}
			buffer.WriteByte(byte(unicode.ToLower(r)))
		} else {
			buffer.WriteByte(byte(r))
		}
	}
	return buffer.String()
}

// 随机字符串
func RandStr(size int, kind int) []byte {
	ikind, kinds, result := kind, [][]int{[]int{10, 48}, []int{26, 97}, []int{26, 65}}, make([]byte, size)
	isAll := kind > 2 || kind < 0
	rand.Seed(time.Now().UnixNano())
	for i := 0; i < size; i++ {
		if isAll {
			ikind = rand.Intn(3)
		}
		scope, base := kinds[ikind][0], kinds[ikind][1]
		result[i] = uint8(base + rand.Intn(scope))
	}
	return result
}
